﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using Com.PhilChuang.Pmvp.Ui.Views.Impl.ViewModels;
using Microsoft.Phone.Controls;

namespace Com.PhilChuang.Pmvp.Ui.Views.Impl.Pages
{
	public sealed partial class AboutPage : AboutPageBase
    {
		#region Events
		public event Action DoLaunchAppRateLink = delegate { };
		public event Action DoOpenSendFeedbackView = delegate { };
		public event Action DoLaunchAppWebsiteLink = delegate { }; 
		#endregion

		#region Instance members
		private readonly ManualResetEvent m_WebBrowserMre = new ManualResetEvent (false);
		private bool m_IsUnloaded = false;
		#endregion

		#region Properties (EMPTY)
		#endregion

		#region Constructors / Init
		public AboutPage ()
        {
            InitializeComponent();
			SetApplicationBarItems ();

			Unloaded += (sender, e) => m_IsUnloaded = true;

			UpdateWebBrowserAsync ();
        }
		#endregion

		#region Event handlers
		/// <summary>
		/// Manual binding from ViewModel to ApplicationBar
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected override void OnViewModelPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			base.OnViewModelPropertyChanged (sender, e);

			if (e.PropertyName == INPCHelper.GetPropertyName (() => ViewModel.HelpContent))
			{
				UpdateWebBrowserAsync ();
			}
		}

		private void g_WebBrowser_Loaded (object sender, RoutedEventArgs e)
		{
			m_WebBrowserMre.Set ();
		}

		private void g_WebBrowser_Navigating (object sender, NavigatingEventArgs e)
		{
			// redirect links to system browser
			e.Cancel = true;
			new Microsoft.Phone.Tasks.WebBrowserTask () { URL = e.Uri.ToString () }.Show ();
		}

		private void g_btn_LaunchAppRateLink_Click (object sender, System.Windows.RoutedEventArgs e)
		{
			DoLaunchAppRateLink ();
		}

		private void g_btn_SendFeedback_Click (object sender, System.Windows.RoutedEventArgs e)
		{
			DoOpenSendFeedbackView ();
		}

		private void g_btn_LaunchAppWebsiteLink_Click (object sender, System.Windows.RoutedEventArgs e)
		{
			DoLaunchAppWebsiteLink ();
		}
		#endregion

		#region Functionality (EMPTY)
		#endregion

		#region Private methods
		private void UpdateWebBrowserAsync ()
		{
			ThreadPool.QueueUserWorkItem (
				obj => {
					try
					{
						if (m_IsUnloaded) return;
						m_WebBrowserMre.WaitOne ();
						if (m_IsUnloaded) return;
						Thread.Sleep (250); // wait just a little longer to ensure the WebBrowser.Loaded event has completed
						if (m_IsUnloaded) return;
						Dispatcher.BeginInvoke (() => {
						                        	try
						                        	{
														if (m_IsUnloaded) return;
														g_WebBrowser.NavigateToString (ViewModel.HelpContent);
						                        	}
						                        	catch (ThreadAbortException)
						                        	{
						                        		// don't care
						                        	}
						                        });
					}
					catch (ThreadAbortException)
					{
						// don't care
					}
				});
		}

		protected override void SetApplicationBarItems ()
		{
			// does nothing
		}
		#endregion

		#region Utility methods (EMPTY)
		#endregion
	}

	public class AboutPageBase : PageBase<AboutPageViewModel>
	{
	}
}
